package fwk.utiles {	

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.display.DisplayObjectContainer;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	
	public class AnimDepthManager
	{

		private var _tabClip:Array;
		private var _listenersActive:Boolean = false;
		private var _stage:DisplayObject;
		
		
		private var _amplitudeX:Number 	= 70;
		private var _amplitudeY:Number 	= 100;
		private var _zoneMove:MovieClip;
		private var _type:Number 		= 1;
		private var _balanceY:Number	= 0;
		private var _balanceX:Number	= 0;
		
		//-----------------------------------------------------------------------
		// Constructeur
		//-----------------------------------------------------------------------
		function AnimDepthManager()
		{
			_tabClip = new Array();
			
		}

		//-----------------------------------------------------------------------
		public function add(clip:MovieClip, profondeur:Number)
		{
			var obj:Object 	= new Object();
			obj.initX 		= clip.x;
			obj.initY 		= clip.y;
			obj.clip 		= clip;
			obj.depth 		= profondeur;
			
			_tabClip.push(obj);
			
		}
		//-----------------------------------------------------------------------
		public function start()
		{
			trace("start " + _tabClip.length);
			if (!_listenersActive)
			{
				_listenersActive 	= true;

				_stage 				= _tabClip[0].clip.stage;
				_stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			}			
		}
		//-----------------------------------------------------------------------
		public function stop()
		{
			if (_listenersActive)
			{
				_listenersActive 	= false;
				_stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				
				for (var i:int = 0; i < _tabClip.length; i++) 
				{
					if(_tabClip[i].transitionX != null) _tabClip[i].transitionX.stop();
					if(_tabClip[i].transitionY != null) _tabClip[i].transitionY.stop();
				}
			}
			
		}
		//--------------------------------------------------------
		private function onMouseMove(event:MouseEvent)
		{
			
			
			var scaleZone1_x:Number = _zoneMove.scaleX;
			var scaleZone1_y:Number = _zoneMove.scaleY;
			var flag:Boolean  = false;
			
			
			if( _zoneMove.mouseX*scaleZone1_x < _zoneMove.width && _zoneMove.mouseX*scaleZone1_x > 0){
				if( _zoneMove.mouseY*scaleZone1_y < _zoneMove.height && _zoneMove.mouseY*scaleZone1_y > 0){
					flag = true;
				}
			}
			
			if (flag)
			{
				
				var ratioX:Number = (_zoneMove.mouseX * scaleZone1_x)/_zoneMove.width ;
				var ratioY:Number = (_zoneMove.mouseY * scaleZone1_y)/_zoneMove.height;
				
				runAnim(ratioX, ratioY);
			}

		}
		//--------------------------------------------------------
		private function runAnim(ratiox:Number, ratioy:Number)
		{

			for (var i:int = 0; i < _tabClip.length; i++) 
			{
				if (_type == 1 || _type == 3)
				{
					if(_tabClip[i].transitionX != null) _tabClip[i].transitionX .stop();
					//anim en axe X
					var ampliCalX:Number = _amplitudeX - ((10 * (_tabClip[i].depth - 1)) / 100) * _amplitudeX;
					var balance:Number = ampliCalX/2  + _balanceX * (ampliCalX/2 );
					var ajout:Number = (balance - ratiox * ampliCalX);
					
					
					_tabClip[i].transitionX = new Tween(_tabClip[i].clip, "x" , Strong.easeOut, _tabClip[i].clip.x, _tabClip[i].initX + ajout, 60, false);
				}
				if (_type == 2 || _type == 3)
				{
					if(_tabClip[i].transitionY != null) _tabClip[i].transitionY.stop();
					//anim en axe Y
					var ampliCalY:Number = _amplitudeY - ((10 * (_tabClip[i].depth - 1)) / 100) * _amplitudeY;
					var balance2:Number = ampliCalY/2  + _balanceY * (ampliCalY/2 );
					var ajout2:Number = (balance2 - ratioy * ampliCalY);
					
					
					_tabClip[i].transitionY = new Tween(_tabClip[i].clip, "y" , Strong.easeOut, _tabClip[i].clip.y, _tabClip[i].initY + ajout2, 60, false);
				}

			}

		}
		
		public function get amplitudeX():Number { return _amplitudeX; }
		
		public function set amplitudeX(value:Number):void 
		{
			_amplitudeX = value;
		}
		
		public function get amplitudeY():Number { return _amplitudeY; }
		
		public function set amplitudeY(value:Number):void 
		{
			_amplitudeY = value;
		}
		
		public function get zoneMove():MovieClip { return _zoneMove; }
		
		public function set zoneMove(value:MovieClip):void 
		{
			_zoneMove = value;
		}
		
		public function get type():Number { return _type; }
		
		public function set type(value:Number):void 
		{
			_type = value;
		}
		
		public function get balanceY():Number { return _balanceY; }
		
		public function set balanceY(value:Number):void 
		{
			_balanceY = value;
		}
		
		public function get balanceX():Number { return _balanceX; }
		
		public function set balanceX(value:Number):void 
		{
			_balanceX = value;
		}
	}
}